//
//  WDJAuthManger.m

#import "WDJAuthManger.h"
#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
@interface WDJAuthManger()<CLLocationManagerDelegate>
@property (nonatomic, strong)CLLocationManager * wdjLocationManager;
@end
@implementation WDJAuthManger
static WDJAuthManger *defaultManager = nil;
+(WDJAuthManger*)defaultManager{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        if(defaultManager == nil){
            defaultManager = [[self alloc] init];
        }
    });
    return defaultManager;
}
- (CLLocationManager *)wdjLocationManager{
    if(!_wdjLocationManager){
        _wdjLocationManager = [[CLLocationManager alloc] init];
        [_wdjLocationManager requestWhenInUseAuthorization];
        [_wdjLocationManager requestAlwaysAuthorization];
        [_wdjLocationManager startUpdatingLocation];
        _wdjLocationManager.delegate = self;
    }
    return _wdjLocationManager;
}
//定位权限
+ (void)requestLocationAuth:(void(^)(void))authBack{
    [WDJAuthManger defaultManager].AuthMangerBlock = authBack;
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    if (authStatus == kCLAuthorizationStatusAuthorizedAlways) {
        [[WDJAuthManger defaultManager].wdjLocationManager startUpdatingLocation];
    }else if (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [[WDJAuthManger defaultManager].wdjLocationManager startUpdatingLocation];
    }else if (authStatus == kCLAuthorizationStatusNotDetermined) {
        [[WDJAuthManger defaultManager].wdjLocationManager requestWhenInUseAuthorization];
    }else{
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"定位权限不被允许" message:@"请前往设置页面开启" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        UIAlertAction * setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [UIApplication.sharedApplication openURL:url options:@{} completionHandler:nil];
            });
        }];
        [alert addAction:cancelAction];
        [alert addAction:setAction];
        [((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController presentViewController:alert animated:YES completion:nil];
    }
}
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *curLocation = locations.firstObject;
    [WDJPublicData defaultManager].wdjLocalLat = [NSString stringWithFormat:@"%f",curLocation.coordinate.latitude];
    [WDJPublicData defaultManager].wdjLocalLon = [NSString stringWithFormat:@"%f",curLocation.coordinate.longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0){
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 city = placemark.administrativeArea;
             }
            [WDJPublicData defaultManager].wdjLocalCityName = city;
            if ([WDJAuthManger defaultManager].AuthMangerBlock) {
                [WDJAuthManger defaultManager].AuthMangerBlock();
            }
            NSLog(@"location -- 省:%@, 市：%@， 区：%@ 街道：%@", placemark.administrativeArea, city, placemark.subLocality, placemark.name);
        }
    }];
}
@end
